package com.lovo.project.servlet;

import com.lovo.project.model.CityConutDO;
import com.lovo.project.model.ShopInfoDO;
import com.lovo.project.service.MapService;
import com.lovo.project.service.impl.MapServiceImpl;
import com.lovo.project.vo.ResultVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@WebServlet(name = "ShowMapServlet",value = "/showMap")
public class ShowMapServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        MapService mapService = new MapServiceImpl();
        List<CityConutDO> list = mapService.findCountByCity();
        List cityShopCountList = new ArrayList();
        for (CityConutDO cityConutDO : list) {
            cityShopCountList.add(new Object[]{cityConutDO.getLongitude(),cityConutDO.getLatitude(), cityConutDO.getCount(), cityConutDO.getName()});
        }
        List<ShopInfoDO> allShopList = mapService.findAllShop();
        System.out.println(allShopList);
        List cityShopList = new ArrayList();
        for (ShopInfoDO shopInfoDO : allShopList) {
            cityShopList.add(new Object[]{shopInfoDO.getLongitude(),shopInfoDO.getLatitude(), shopInfoDO.getName(),shopInfoDO.getLocation()});
        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("count",cityShopCountList);
        map.put("shop",cityShopList);

        response.getWriter().println(ResultVO.success().add(map).toJson());
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
